/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove301.jianghongtiao.liaad.prisoner.memory;

import cern.colt.Arrays;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import sk.lieskove301.jianghongtiao.liaad.prisoner.strategy.FightResultEnum;

/**
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 */
public class Mems2RPlot {

    private static String vals2Str(boolean[] vals){
        String result = "";
        for (int i = 0; i < vals.length; i++) {
            boolean b = vals[i];
            if(b){
                result += "\"C\", ";
            } else {
                result += "\"B\", ";
            }
        }
        return result.substring(0, result.length()-2);
    }
    
    private static void printText(String forgettingName, boolean[] values, String decis){
            System.out.println("arr<-c(" + decis + ")");
            System.out.println("plot(arr, type=\"l\", ylim=c(0, 1), axes=FALSE"
                    + ", ylab=\"Agent's decision\", "
                    + "xlab=\"Old -> New memory\", "
                    + "main=\"" + forgettingName + "\")");
            System.out.println("axis(1, at=1:"+values.length+", lab=c("+vals2Str(values)+"))");
            System.out.println("axis(2, las=1, at=0:1, lab=c(\"Betr\", \"Coop\"))");
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        boolean[] values = new boolean[]{true, true, false, false, true, true, true, true, true, false};
        boolean[] values = new boolean[]{true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
        System.out.println("par(mfrow=c(2,2))");
        for (int i = 0; i < ForgettingEnum.values().length; i++) {
            Forgetting memory = ForgettingEnum.getById(i).value();
            String pessimistic = "";
            String optimistic = "";
            String pessimisticInv = "";
            String optimisticInv = "";

            for (int j = 0; j < values.length; j++) {
                Deque deq = new ArrayDeque();
                boolean b = values[j];
                if (b) {
                    deq.add(FightResultEnum.BothCooperate);
                } else {
                    deq.add(FightResultEnum.BothBetray);
                }
                String be = Integer.toBinaryString(memory.countBetrayMemories(deq).binValue());
                String co = Integer.toBinaryString(memory.countCooperateMemories(deq).binValue());
                
                pessimistic += (be.equals("0")?"0":"1")+", ";
                optimistic  += (co.equals("0")?"0":"1")+", ";
                pessimisticInv += (be.equals("0")?"1":"0")+", ";
                optimisticInv  += (co.equals("0")?"1":"0")+", ";
                
            }
            pessimistic = pessimistic.substring(0, pessimistic.length()- 2);
            optimistic = optimistic.substring(0, optimistic.length()- 2);
            pessimisticInv = pessimisticInv.substring(0, pessimisticInv.length()- 2);
            optimisticInv = optimisticInv.substring(0, optimisticInv.length()- 2);
            
            System.out.println("#"+memory.getName());
            printText("Optimistic", values, optimistic);
            printText("Pessimistic", values, pessimistic);
            printText("Inverted optimistic", values, optimisticInv);
            printText("Inverted pessimistic", values, pessimisticInv);
            System.out.println("");
            System.out.println("");
            //System.out.println("abline(h=" + (1 - OFI) * 100 + ", col=\"red\")");
        }
    }
}
